<?php
/**
 * Created by PhpStorm.
 * User: haozhenxing
 * Date: 16/8/26
 * Time: 下午10:53
 */
//封装请求方法
function request($url,$https=true,$method='get',$data=null){
    //1.初始化url
    $ch = curl_init($url);//dump($ch);die;
    //2.设置相关的参数
    //字符串不直接输出,进行一个变量的存储
    $a = curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //判断是否为https请求
    if($https === true){
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    }
    //判断是否为post请求
    if($method == 'post'){
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    //3.发送请求
    $str = curl_exec($ch);//dump($str);die;
    //4.关闭连接
    curl_close($ch);

    $res = json_decode($str,true);
    //返回请求到的结果
    return $res;
}

//post
function curlPost($url,$postFields){
    //dump($url);die;
    $postFields = http_build_query($postFields);

    $ch = curl_init ();
    curl_setopt ( $ch, CURLOPT_POST, 0 );
    curl_setopt ( $ch, CURLOPT_HEADER, 0 );
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt ( $ch, CURLOPT_URL, $url );
    curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postFields );
    $result = curl_exec ( $ch );
    curl_close ( $ch );
    return $result;
}

//方法一 :远程请求接口,以下两种方法均可以
function curl($url){

    //$url = 'http://api.jisuapi.com/vehiclelimit/query?appkey=4e5a8da273b73b42&city='.$cityCode;

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    // 执行HTTP请求
    curl_setopt($ch , CURLOPT_URL , $url);

    $res = curl_exec($ch);

    $res = json_decode($res,true);

    return $res;
}

//方法二 :远程请求网页
function curlRequest($url){

    //$url = 'http://api.jisuapi.com/vehiclelimit/query?appkey=4e5a8da273b73b42&city='.$cityCode;

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 360);

    // 执行HTTP请求
    curl_setopt($ch , CURLOPT_URL , $url);

    $res = curl_exec($ch);

    //4.关闭连接
    curl_close($ch);

    

    return $res;
}

//邮件发送测试方法
function sendMail($subject,$msghtml,$sendAddress){
    // //因为如果发送到的地址已经为空了，我们就没必要去加载类文件
    if($sendAddress == ''){
        //邮件为空，返回false;
        return false;
    }
    //引入发送类phpmailer.php
    require './PHPMailer/class.phpmailer.php';
    //实列化对象
    $mail             = new PHPMailer();
    /*服务器相关信息*/
    $mail->IsSMTP();                        //设置使用SMTP服务器发送
    $mail->SMTPAuth   = true;               //开启SMTP认证
    $mail->Host       = 'smtp.163.com';         //设置 SMTP 服务器,自己注册邮箱服务器地址
    $mail->Username   = 'hzxphper@163.com';      //发信人的邮箱用户名
    $mail->Password   = 'rabbit0077';          //发信人的邮箱密码
    /*内容信息*/
    $mail->IsHTML(true);               //指定邮件内容格式为：html
    $mail->CharSet    ="UTF-8";          //编码
    $mail->From       = 'hzxphper@163.com';       //发件人完整的邮箱名称
    $mail->FromName   ="郝振兴";      //发信人署名
    $mail->Subject    = $subject;         //信的标题
    $mail->MsgHTML($msghtml);           //发信主体内容
    // $mail->AddAttachment("fish.jpg");      //附件
    /*发送邮件*/
    $mail->AddAddress($sendAddress);        //收件人地址
    //使用send函数进行发送
    if($mail->Send()) {
        //发送成功返回真
        return true;
        // echo '您的邮件已经发送成功！！！';
    } else {
        return $mail->ErrorInfo;//如果发送失败，则返回错误提示
    }
}

function add_material($file_info){
    //$access_token=get_access_token();
    $url="http://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=BOKHkNbIrzCdgdrf09V37D1WdN-ikZX9SlAd8lGMie7ZgiEgpn3BCFkxjzNNqLBAe70c3raKBAqFpFRWojRqcLkYrSQRtWZ4oOy91vSlbStWB4GkyRL3Xxvt8lRtU2fGKZShAAACRK";
    $ch1 = curl_init ();
    $timeout = 5;
    $real_path="{$_SERVER['DOCUMENT_ROOT']}{$file_info['filename']}";
    //$real_path=str_replace("/", "\\", $real_path);
    $data= array("media"=>"@{$real_path}",'form-data'=>$file_info);
    curl_setopt ( $ch1, CURLOPT_URL, $url );
    curl_setopt ( $ch1, CURLOPT_POST, 1 );
    curl_setopt ( $ch1, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt ( $ch1, CURLOPT_CONNECTTIMEOUT, $timeout );
    curl_setopt ( $ch1, CURLOPT_SSL_VERIFYPEER, FALSE );
    curl_setopt ( $ch1, CURLOPT_SSL_VERIFYHOST, FALSE );
    curl_setopt ( $ch1, CURLOPT_POSTFIELDS, $data );
    $result = curl_exec ( $ch1 );//dump($result);die;
    curl_close ( $ch1 );
    if(curl_errno()==0){
        $result=json_decode($result,true);
        //dump($result);die;
        return $result['url'];
    }else {
        return false;
    }
}

//文件下载函数
function download($url){

    //$url = 'http://mat1.gtimg.com/sports/soccerdata/images//player/27581.jpg';
    $img = file_get_contents($url);
    $fileName = rand(100,10000);
    $downfile = file_put_contents("./Public/home/uploads/$fileName.jpg", $img);


}

//自定义删除
function my_del($table,$where)
{

    $model=M($table)->where($where);
    if (C('IS_FALSE_DEL')) {
        $res=$model->save(['is_delete'=>'1']);   
    }else{
        $res=$model->delete();
    }   
   
    return $res;
}

function my_page($model,$page,$page_size)
{
   
    $model1=clone $model;
    $model2=clone $model;
    $count=$model1->count();

    $total_page = ceil($count / $page_size);

    $data=$model2->page("{$page},{$page_size}")->select();

    $code=!empty($data) ? true : false;

    return [
        'count'         =>  $count,
        'total_page'    =>  $total_page,
        'data'          =>  $data,
        'code'          =>  $code,
    ]; 
}

function build_order_no() {    
    return 'FP'.date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid().mt_rand(10,20), 7, 13), 1))), 0, 8);
}


/**
 * 返回加密后的密码字符串
 * @param unknown_type $password
 */
function getPwdEncodeString($password){
    return md5(md5($password).substr($password,0,3));
}

// 获取时间差
function timediff( $begin_time, $end_time )
{
  
    $begin_time=mktime(0,0,0,date("n",$begin_time),date("j",$begin_time),date("Y",$begin_time));

    $end_time=mktime(0,0,0,date("n",$end_time),date("j",$end_time),date("Y",$end_time));

    if ( $begin_time < $end_time ) {
        $starttime = $begin_time;
        $endtime = $end_time;
    } else {
        $starttime = $end_time;
        $endtime = $begin_time;
    }
    $timediff = $endtime - $starttime;
    $days = intval( $timediff / 86400 );
    $remain = $timediff % 86400;
    $hours = intval( $remain / 3600 );
    $remain = $remain % 3600;
    $mins = intval( $remain / 60 );
    $secs = $remain % 60;
    $res = array( "day" => $days, "hour" => $hours, "min" => $mins, "sec" => $secs );
    // return $res;
    return $days;
}


 























